import io
import os.path
import subprocess
import sys

import docx2pdf
from docx import Document
import PyPDF2

def download(doc: Document, context):
    # 创建一个 BytesIO 对象
    from io import BytesIO
    from flask import send_file

    if context['type'] == 'pdf':
        if not os.path.isdir(context['out_path']):
            os.mkdir(context['out_path'])
        doc.save(context['out_path'] + '/' + context['filename'] + '.docx')
        print('生成docx成功')

        convert_to_pdf(context['out_path'] + '/' + context['filename'] + '.docx',
                       context['out_path'] + '/' + context['filename'] + '.pdf')

        res = send_file(
            context['out_path'] + '/' + context['filename'] + '.pdf',
            download_name=context['filename'] + '.pdf',
            as_attachment=True
        )
        return res
    else:
        file_io = BytesIO()
        # 将 docx 对象保存到 BytesIO 对象中
        doc.save(file_io)
        file_io.seek(0)
        res = send_file(
            file_io,
            download_name=context['filename'] + '.docx',
            as_attachment=True
        )
        return res

def convert_to_pdf(input_file, output_file):
    if sys.platform == 'win32':
        try:
            import win32com
            from docx2pdf import convert
            convert(input_file,output_file)
        except ImportError:
            print("在非 Windows 环境下无法导入 win32com.client 模块")
    else:
        # 在 Linux 或其他系统中执行的操作
        cmd = f'pandoc {input_file} -s -o {output_file}'
        print(f'执行的cmd:{cmd}')
        # cmd = f'unoconv -f pdf {input_file}'
        subprocess.run(cmd, shell=True)
        print("转换成功")
# def add(doc,pdf):
#     # 获取所有段落
#     paragraphs = doc.paragraphs
#     for paragraph in paragraphs:
#         # 获取段落文本
#         text = paragraph.text
#         pdf.drawString(paragraph.height,paragraph.width,text)
#         print(text)
#         # 检查段落中是否包含图表
#         if paragraph.tables:
#             # 获取图表对象
#             for t in paragraph.tables:
#                 # 创建线图
#                 lp = LinePlot()
#                 lp.x = 50
#                 lp.y = 50
#                 lp.width = 300
#                 lp.height = 150
#                 lp.data = [data]
#                 lp.lineLabelFormat = "%.2f"
#                 lp.strokeColor = colors.blue
#
#                 # 添加图表到绘图
#                 drawing.add(lp)
#         # 检查段落中是否包含图像
#         for run in paragraph.runs:
#             if run._element.xpath('.//pic:pic'):
#                 # 获取图片对象
#                 pic = run._element.xpath('.//pic:pic')[0]
#                 pdf.drawInlineImage(pic, pic.height, pic.width)
#     # 获取所有图像
#     for paragraph in doc.paragraphs:
#         if paragraph.runs:
#             for run in paragraph.runs:
#                 for inline in run.inline_shapes:
#                     if inline.has_picture:
#                         # 图像文件的路径
#                         image_path = inline.picture.filename
#                         print(image_path)
#
#     # 获取所有图表
#     for shape in doc.inline_shapes:
#         if shape.has_chart:
#             chart = shape.chart
#             # 在这里可以进一步处理图表，如获取数据等
#             print(chart.chart_title.text_frame.text)




